package com.jmectf.net.console;

import java.io.IOException;
import java.net.Socket;

import com.jme.math.Vector3f;
import com.jmectf.net.BattleServer;
import com.jmectf.net.PlayerData;

public class ServerThread extends Thread {

	BattleServer server;
	ServerConsole console;
	ServerInfo info;

	public ServerThread(ServerConsole console, ServerInfo info) {
		this.console = console;
		this.server = new BattleServer(this);
		this.info = info;
		this.info = new ServerInfo();
		console.toFront();
	}

	public void close() {
		try {
			server.close();
			info.dispose();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void run() {
		try {
			server.run();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void update(PlayerData data) {
		String buffer = "ID\tIP\t\tPosition\n";
		int ID = 1;
		for(int i = 0; i < data.getConnections().size(); i++) {
			Socket c = data.getConnections().get(i);
			Vector3f pos = data.getPositions().get(i);
			info.getStatusArea().setText(buffer + '\n' + ID++ + "\t" + c.getInetAddress() + "\t(" + 
					(int)pos.x + "," + (int)pos.y + "," + (int)pos.z + ")\n");
		}
		
	}

	public void update(String s) {
		info.getMessageArea().append('\n' + s);
	}

}
